using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Controllers
{
	/// <summary>
	/// 科室管理控制器（读操作）
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class DepartmentManagementController : ControllerBase
	{
		private readonly IMediator _mediator;
		
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator">MediatR中介者</param>
		public DepartmentManagementController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 获取科室详情
		/// </summary>
		/// <param name="departmentId">科室ID</param>
		/// <returns>科室详细信息</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/DepartmentManagement/GetById?departmentId=1
		/// </remarks>
		/// <response code="200">查询成功，返回科室详细信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">科室不存在</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<DepartmentDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		[HttpGet]
		public async Task<ApiResult<DepartmentDto>> GetById([FromQuery] long departmentId)
		{
			try { var data = await _mediator.Send(new GetDepartmentByIdQuery { DepartmentId = departmentId }); return new ApiResult<DepartmentDto> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<DepartmentDto> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 获取科室列表（支持分页和筛选）
		/// </summary>
		/// <param name="nameKeyword">科室名称关键词（模糊搜索）</param>
		/// <param name="isKeyDepartment">是否重点科室筛选</param>
		/// <param name="isEmergencyAvailable">是否开通急诊服务筛选</param>
		/// <param name="page">页码（从1开始，默认值：1）</param>
		/// <param name="size">每页数量（默认值：10）</param>
		/// <returns>分页的科室列表</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/DepartmentManagement/GetList?nameKeyword=内科&amp;isKeyDepartment=true&amp;page=1&amp;size=10
		/// </remarks>
		/// <response code="200">查询成功，返回分页的科室列表</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<ApiPaging<DepartmentDto>>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		[HttpGet]
		public async Task<ApiResult<ApiPaging<DepartmentDto>>> GetList([FromQuery] string? nameKeyword = null, [FromQuery] bool? isKeyDepartment = null, [FromQuery] bool? isEmergencyAvailable = null, [FromQuery] int page = 1, [FromQuery] int size = 10)
		{
			try { var data = await _mediator.Send(new GetDepartmentsListQuery { NameKeyword = nameKeyword, IsKeyDepartment = isKeyDepartment, IsEmergencyAvailable = isEmergencyAvailable, Page = page, Size = size }); return new ApiResult<ApiPaging<DepartmentDto>> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<ApiPaging<DepartmentDto>> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 按科室获取疾病列表（支持分页和筛选）
		/// </summary>
		/// <param name="departmentId">科室ID（必填）</param>
		/// <param name="nameKeyword">疾病名称关键词（模糊搜索，可选）</param>
		/// <param name="page">页码（从1开始，默认值：1）</param>
		/// <param name="size">每页数量（默认值：10）</param>
		/// <returns>分页的疾病列表</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/DepartmentManagement/GetDiseases?departmentId=1&amp;nameKeyword=感冒&amp;page=1&amp;size=10
		/// </remarks>
		/// <response code="200">查询成功，返回分页的疾病列表</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">科室不存在</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<ApiPaging<DiseaseDto>>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		[HttpGet]
		public async Task<ApiResult<ApiPaging<DiseaseDto>>> GetDiseases([FromQuery] long departmentId, [FromQuery] string? nameKeyword = null, [FromQuery] int page = 1, [FromQuery] int size = 10)
		{
			try { var data = await _mediator.Send(new GetDiseasesByDepartmentQuery { DepartmentId = departmentId, NameKeyword = nameKeyword, Page = page, Size = size }); return new ApiResult<ApiPaging<DiseaseDto>> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<ApiPaging<DiseaseDto>> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}
	}
}
